import constants from '@/constants';
import { ClusterService } from '@/core/cluster.service';
import { getHash, getIPAddress } from '@/utils';
import fastifyMultipart from '@fastify/multipart';
import { Logger, ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import { PrismaService } from './core/prisma.service';
import dayjs from 'dayjs';

async function main() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
  );
  const config = app.get(ConfigService);
  const prisma = app.get(PrismaService);

  app.enableCors();
  app.setGlobalPrefix(config.get('prefix'));
  app.useGlobalPipes(new ValidationPipe(config.get('validation')));
  app.register(fastifyMultipart, config.get('multipart'));

  await app.listen(config.get('port'), config.get('host'), async () => {
    Logger.debug(`
      Server running at:
      - Local:              ${await app.getUrl()}
      - Network:            http://${getIPAddress()}:${config.get('port')}
      - Hash:               ${await getHash(constants.default.pass)}
      - Name:               ${config.get('name')}      
      - BlockChain:         ${config.get('blockchain')}      
      - Payment:            ${config.get('payment')}      
      - UseBalance:         ${config.get('isUseBalance')}      
    `);
  });
}

constants.isDev ? main() : ClusterService.clusterize(main);
